<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Created by PhpStorm.
 * Date: 2018/12/25
 * Author: Zhangzhenbei <zhenbeigg@163.com>
 */
namespace app\api\controller;
use think\Db;
class Goods extends Common
{
    /**
    *商品列表
    *@access public 
    *@param int $category_id 分类id
    *@param int $page 分页
    */
    public function get_goods_list()
    {
        $where = ['status'=>0];
        $category_id = input('param.category_id');
        $page = input('param.page');
        if(!$page || !$category_id){
            json_msg(1,'请求错误');
        }
        $per_page = 10;
        $page = $page-1;
        $where['category_id']=$category_id;        
        $list = Db::name('goods')->where($where)->order('id desc')->limit($per_page*$page,$per_page)->field('id,title,img_url,sell_price,market_price')->select();
        $list = edit_img_url($list,'img_url',$type=1);
        if($list){
            json_msg_data(0,'ok',$list);
        }else{
            json_msg(1, '没有更过数据');
        }
    }
    /**
    *分类列表
    *@access public 
    */
    public function get_category_list()
    {
        $category = Db::name('sys_category')->where('channel_id',2)->field('id,title')->select();
        if($category){
            json_msg_data(0,'ok',$category);
        }else{
            json_msg(1, '没有更过数据');
        }
    }
    /**
    *商品详情
    *@access public 
    *@param int $goods_id 商品id
    *@param int $user_id 用户id
    */
    public function get_goods_id()
    {
        $goods_id = input('param.goods_id');
        $user_id = input('param.user_id');
        if(!$goods_id || !$user_id){
            json_msg(1, '请求错误');
        }
        $info = Db::name('goods')->where('id',$goods_id)->field('id,title,sell_price,market_price,content')->find();
        if(!$info){
            json_msg(1, '商品不存在');
        }
        $imglist = Db::name('img')->where(['type'=>1,'main_id'=>$goods_id])->field('img_url')->select();
        $info['imglist'] = edit_img_url($imglist,'img_url',1);
        //购物车商品数量
        $cartCount = get_cart_count($user_id);
        $info['cartCount'] = $cartCount;
        //推荐二维码
        $savepath = 'public/upload/usergoodsqcode';//存放在当前目录的upload文件夹下
        $savename = get_user_info_data($user_id,'user_name').'_'.$goods_id.".jpg";
        $path = $savepath.'/'.$savename;
        $url = sysconf('website')."/index/goods/show/id/".$id."/parentId/".$user_id;
        $qrcode = $this->createQrcode($url,$path);
        $info['qrcode'] = $qrcode;
        $info = edit_img_url($info,'qrcode',2);
        json_msg_data(0,'ok',$info);
    }
}